<?php
/**
 * 公共函数库 - JBlog
 * 
 * @copyright (c) 2008-2012 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: Functions.php 21 2012-07-12 05:08:19Z lisijie86@gmail.com $
*/

function M($name, $id = 0, $userid = 0) {
	static $_objects = array();
	$name = ucfirst(strtolower($name));
	$key = "{$name}_{$id}_{$userid}";
	if ($id == 0 || !isset($_objects[$key])) {
		$_objects[$key] = new $name($id, $userid);
	}
	return $_objects[$key];
}

function URL($ca = '', $qs = '') {
	if (empty($ca)) {
		$ca = CONTROL_NAME . "." . ACTION_NAME;
	}
	return Router::getInstance()->url($ca, $qs);
}

// 记录和统计时间（微秒）
function ET($start, $end='', $dec=4) {
    static $_info = array();
    if(!empty($end)){ // 统计时间
        if(!isset($_info[$end])) $_info[$end]   =  microtime(TRUE);
        return number_format(($_info[$end]-$_info[$start]),$dec);
    }else{ // 记录时间
        $_info[$start]  =  microtime(TRUE);
    }
}

function __($string) {
	$args = func_get_args();
	return call_user_func_array('sprintf', $args);
}

function _e($string) {
	$args = func_get_args();
	echo call_user_func_array('__', $args);
}
